package com.ddwl.goods.dao;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.constant.GoodsEnum;
import com.ddwl.common.dao.TkDao;
import com.ddwl.common.util.MySqlUtil;
import com.ddwl.goods.mapper.CategoryMapper;
import com.ddwl.goods.model.Category;
import com.ddwl.schema.bo.goods.CategorySearchBo;
import com.ddwl.schema.vo.goods.retail.ShopItemCategoryPageVo;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author hemiaojun
 * @Date 2020/11/30 10:52
 */
@Repository
public class CategoryDao extends TkDao<CategoryMapper, Category> {

    @Resource
    private CategoryMapper categoryMapper;

    public List<ShopItemCategoryPageVo> treeShopItemCateGory(Category category) {
        return categoryMapper.treeShopItemCateGory(category);
    }

    public List<Category> listShopItemCateGory(String shopId) {
        Example example = getExample();
        example.and().andEqualTo("shopId", shopId)
                .andEqualTo("effect", BaseEnum.StatusEnum.ENABLE.getCode());
        example.orderBy("sort").asc().orderBy("gmtCreated").desc();
        return categoryMapper.selectByExample(example);
    }

    public List<Category> findChildrenList(String id) {
        Category entity = new Category();
        entity.setPid(id);
        return this.select(entity);
    }

    public List<Category> findTopMall() {
        Example example = getExample();
        example.and().andIsNull("pid")
                .orEqualTo("pid","");
        example.and().andEqualTo("categoryType", GoodsEnum.CategoryTypeEnum.PLATFORM.getCode())
                .andEqualTo("effect", BaseEnum.StatusEnum.ENABLE.getCode());
        example.orderBy("hotFlag").desc().orderBy("sort").asc();
        return categoryMapper.selectByExample(example);
    }

    public List<Category> selectByParam(CategorySearchBo param) {
        Example example = getParamExample(param);
        return categoryMapper.selectByExample(example);
    }

    public List<Category> findChildren(CategorySearchBo param) {
        Example example = getParamExample(param);
        example.orderBy("hotFlag").desc().orderBy("sort").asc();
        return categoryMapper.selectByExample(example);
    }

    private Example getParamExample(CategorySearchBo param) {
        Example example = getExample();
        example.and().andEqualTo("mchId", param.getMchId())
                .andEqualTo("id", param.getId())
                .andLike("name", MySqlUtil.allLike(param.getName()))
                .andEqualTo("categoryType", GoodsEnum.CategoryTypeEnum.PLATFORM.getCode());
        example.and().andEqualTo("pid", param.getPid())
                .orEqualTo("id", param.getPid());
        return example;
    }

    public List<Category> getSubListByPid(String id) {
        Category category = new Category();
        category.setPid(id);
        return this.select(category);
    }

    public Category getCateWithShopId(String id, String shopId) {
        Example example = getExample();
        example.and().andEqualTo("id", id)
                .andEqualTo("shopId", shopId);
        return categoryMapper.selectOneByExample(example);
    }

    public List<Category> listForMerchant(String shopId) {
        Example example = getExample();
        example.and().andEqualTo("shopId", shopId);
        example.orderBy("sort").asc().orderBy("gmtCreated").desc();
        return categoryMapper.selectByExample(example);
    }

    public List<Category> selectList(String shopId) {
        Example example = getExample();
        example.and().andEqualTo("shopId", shopId)
                .andEqualTo("effect", BaseEnum.StatusEnum.ENABLE.getCode());
        example.orderBy("sort").asc().orderBy("gmtCreated").desc();
        return categoryMapper.selectByExample(example);
    }

    public List<Category> getAll() {
        return categoryMapper.selectAll();
    }

    public void changeCategoryCode(Map<String, String> map) {
        categoryMapper.changeCategoryCode(map);
    }

    public void updateEffectByIds(Integer down, List<String> categoryIdList) {
        Category category = new Category();
        category.setEffect(down);
        Example example = getExample();
        example.and().andIn("id", categoryIdList);
        categoryMapper.updateByExampleSelective(category, example);
    }

    public List<Category> getDuplicateName(String shopId, String name, String id) {
        Example example = getExample();
        example.and().andEqualTo("shopId", shopId)
                .andEqualTo("name", name)
                .andNotEqualTo("id", id);
        return categoryMapper.selectByExample(example);
    }

    public List<Category> topListForMerchant(String shopId) {
        Example example = getExample();
        example.and().andEqualTo("shopId", shopId);
        example.and().andIsNull("pid")
                .orEqualTo("pid", "");
        example.orderBy("sort").asc().orderBy("gmtCreated").desc();
        return categoryMapper.selectByExample(example);
    }
}
